<?php
namespace prog_index {
    use pfp\lib as pfplib;
    use prog_index\lib as progLib;
    
    \defined( '_JEXEC' ) or die( 'Restricted access' );

    /**
     * Description of program Init
     *
     * @author George Zeakis<zeageorge@google.com>
     */
    class indexInit extends pfplib\progInit {

        private   $_events                 = array();
        private   $_dataToGiveToController = array();
        private   $_alreadyLoaded = \FALSE;
        /**
         * 
         * @return \prog_index\indexInit
         * @throws pfplib\codeExecution_FlowControlException
         */
        public function Init(){
            $tmp = __FUNCTION__."_tmp"; if (isset($this->_vars["$tmp"])){ unset($this->_vars["$tmp"]); }
            try {
                if ($this->_alreadyLoaded){ return (array)$this->_dataToGiveToController; } else { $this->_alreadyLoaded = \TRUE; }
                $this->registerEvents();
                $this->_vars["$tmp"]["prog_entry_point"] = \dirname(__FILE__) .DS;
                require_once $this->_vars["$tmp"]["prog_entry_point"]."libs".DS."Config.php";
                $this->_vars["programConfig"] = new progLib\Config(__NAMESPACE__, 
                                                                $this->_vars["$tmp"]["prog_entry_point"],
                                                                $this->_vars["$tmp"]["prog_entry_point"]."config_file.php", 
                                                                $this->config["programName"]);
                
                $eventData = array('event_name'=>$this->_events['event_onBefore_Init'],'args'=>&$this->_vars); pfplib\eventManager::riseEvent($eventData);
                
                pfplib\Autoloader::registerNamespace($this->_vars["programConfig"]->namespaces());

                $this->_vars["$tmp"]["dataToGiveToController"]["user"]           = $this->user; // pfpLib\User object
                $this->_vars["$tmp"]["dataToGiveToController"]["dbAdapter"]      = $this->dbAdapter; // pfpLib\databaseAdapter object
                $this->_vars["$tmp"]["dataToGiveToController"]["config"]         = $this->config; // array('dbAdapter_MainDatabaseConnectionData'=>deepDynamicContainer object, 'inProductionMode'=>boolean, requestObject_type'=>string (eg. http_GET_Request), 'requestObject'=>mix depending on requestObject_type)
                $this->_vars["$tmp"]["dataToGiveToController"]["program_config"] = $this->_vars["programConfig"]; // pfpLib\deepDynamicContainer
                
                $eventData = array('event_name'=>$this->_events['event_onAfter_Init'],'args'=>&$this->_vars); pfplib\eventManager::riseEvent($eventData);
                
                $this->_dataToGiveToController = $this->_vars["$tmp"]["dataToGiveToController"]; unset($this->_vars["$tmp"]);
                return (array)$this->_dataToGiveToController;
            } catch (pfplib\codeExecution_FlowControlException $exc) {
                $this->_vars["$tmp"]["errors"] = $exc;
                $eventData = array('event_name'=>$this->_events['event_onError'],'args'=>&$this->_vars); pfplib\eventManager::riseEvent($eventData);
                unset($this->_vars["$tmp"]); 
                throw new pfplib\codeExecution_FlowControlException(array('return'=>\FALSE), $exc->getMessage()); return \FALSE;
            }
        }
        
        private function registerEvents(){
            $this->_events = array(
                'event_onStart'   =>'event_onStart',
                'event_onError'   =>'event_onError',
                'event_onWarning' =>'event_onWarning',
                'event_onBefore_Init'=>'event_onBefore_Init',
                'event_onAfter_Init'=>'event_onAfter_Init',
            );
            pfplib\eventManager::registerEvents($this->_events);
        }
    }
}
?>
